package com.cxlin.cwanandroid.ui.home.viewmodel

import androidx.lifecycle.viewModelScope
import com.cxlin.base.BaseViewModel
import com.cxlin.base.model.api.Result
import com.cxlin.base.mvi.UiEvent
import com.cxlin.base.mvi.UiIntent
import com.cxlin.base.mvi.UiState
import com.cxlin.cwanandroid.model.bean.ArticleList
import com.cxlin.cwanandroid.model.repository.HomeRepository
import com.cxlin.cwanandroid.model.repository.ProjectRepository
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/4/30 11:35
 */
class ProjectTypeViewModel(val repository: ProjectRepository, val homeRepository: HomeRepository) :
    BaseViewModel() {

    sealed class ProjectTypeUiState : UiState {
        object Init : ProjectTypeUiState()
        data class IsRefresh(val isRefresh: Boolean) : ProjectTypeUiState()
        data class FetchArticlesSuccess(val articles: ArticleList?) : ProjectTypeUiState()
    }

    sealed class ProjectTypeEvent : UiEvent {
        class ToastEvent(val message: String, val short: Boolean = false) : ProjectTypeEvent()
    }

    sealed class ProjectTypeUiIntent : UiIntent {
        data class GetArticleList(val isRefresh: Boolean = false) : ProjectTypeUiIntent()
        data class CollectArticle(val articleId: Int, val boolean: Boolean) : ProjectTypeUiIntent()
    }

    private var currentPage = 0

    override fun handleIntent(intent: UiIntent) {
        when (intent) {
            is ProjectTypeUiIntent.GetArticleList -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.isRefresh) {
                            currentPage = 0
                            mUiStateFlow.update {
                                ProjectTypeUiState.IsRefresh(true)
                            }
                        }
                        val result = repository.getLastedProject(currentPage)
                        if (result is Result.Success) {
                            mUiStateFlow.update{
                                ProjectTypeUiState.IsRefresh(false)
                            }
                            mUiStateFlow.update{
                                ProjectTypeUiState.FetchArticlesSuccess(result.data)
                            }
                            currentPage += 1
                        } else if (result is Result.Error) {
                            mUiStateFlow.update{
                                ProjectTypeUiState.IsRefresh(false)
                            }
                            mEffectFlow.emit(
                                ProjectTypeEvent.ToastEvent(
                                    result.exception.message ?: "网络异常",
                                    true
                                ),
                            )

                        }
                    }
                }
            }

            is ProjectTypeUiIntent.CollectArticle -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.boolean) {
                            homeRepository.collectArticle(intent.articleId)
                        } else {
                            homeRepository.cancelCollectArticle(intent.articleId)
                        }

                    }
                }
            }
        }
    }

    override fun createInitState(): UiState {
        return ProjectTypeUiState.Init
    }
}